﻿using System;
using System.Collections.Generic;

namespace Douya.Json
{
    public class ObjectBuilder
    {
        Dictionary<String, IJsonValue> map;

        public ObjectBuilder()
        {
            map = new Dictionary<String, IJsonValue>();
        }

        public int Count
        {
            get { return map.Count; }
        }

        public ObjectBuilder AddNull(string name)
        {
            map[name] = new JsonNull();

            return this;
        }

        public ObjectBuilder Add(string name, bool value)
        {
            map[name] = new JsonBoolean(value);

            return this;
        }

        public ObjectBuilder Add(string name, int value)
        {
            map[name] = new JsonInteger(value);

            return this;
        }

        public ObjectBuilder Add(string name, string value)
        {
            if (value != null)
            {
                map[name] = new JsonString(value);
            }
            else
            {
                map[name] = new JsonNull();
            }

            return this;
        }

        public ObjectBuilder Add(string name, JsonObject value)
        {
            if (value != null)
            {
                map[name] = value;
            }
            else
            {
                map[name] = new JsonNull();
            }

            return this;
        }

        public ObjectBuilder Add(string name, JsonArray value)
        {
            if (value != null)
            {
                map[name] = value;
            }
            else
            {
                map[name] = new JsonNull();
            }

            return this;
        }

        public ObjectBuilder Add(string name, IJsonValue value)
        {
            if (value != null)
            {
                map[name] = value;
            }
            else
            {
                map[name] = new JsonNull();
            }

            return this;
        }

        public JsonObject Build()
        {
            return new JsonObject(map);
        }

        public override string ToString()
        {
            return Build().ToString();
        }
    }
}
